<?php

abstract class BaseValidator {
	public $options = array();
	public $messages = array();
	
	public function __construct($options = array(), $messages = array()) {
		$this->options['required'] = true;
		$this->messages['required'] = 'Поле необходимо заполнить';
		$this->messages['invalid'] = 'Поле заполнено неверно';
		
		$this->options = array_merge($this->options, $options);
		$this->messages = array_merge($this->messages, $messages);
	}
	
	public function clean($value) {
		if ($this->isEmpty($value) && $this->options['required']) {
			throw new ValidatorError($this, 'required');
		}
		return $this->doClean($value);
	}
	
	abstract public function doClean($value); // Implemented by subclasses
	
	public function addError($name, $parameters) {
		$this->errors[$name] = $parameters;
	}

	public function isEmpty($value) {
		return empty($value);
	}
	
}
